package com.haifeng.lyw.blerobot.search;

/**
 * Created by zhangyuanyuan on 2017/7/5.
 */

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.haifeng.lyw.blerobot.R;

import java.util.ArrayList;

/**
 * @author 广州汇承信息科技有限公司
 * @Description: TODO<自定义适配器Adapter,作为listview的适配器>
 * @data: 2014-10-12 上午10:46:30
 * @version: V1.0
 * 暂时不用该类
 */
public class LeDeviceListAdapter extends BaseAdapter {
    private final ArrayList<Integer> rssis;
    private ArrayList<BluetoothDevice> mLeDevices;
    private Context context;
    private SharedPreferences sp = null;


    public LeDeviceListAdapter(Context context) {
        super();
        rssis = new ArrayList<>();
        mLeDevices = new ArrayList<>();
        this.context = context;
        sp = context.getSharedPreferences("blue_tooth_device",Context.MODE_PRIVATE);
    }

    public void addDevice(BluetoothDevice device, int rssi) {
        if (!mLeDevices.contains(device)) {
            mLeDevices.add(device);
            rssis.add(rssi);
            notifyDataSetChanged();
        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        mLeDevices.clear();
        rssis.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i) {
        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 重写getview
     **/
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        // General ListView optimization code.
        // 加载listview每一项的视图
        ViewHodler mHodler;
        if (view == null) {
            mHodler = new ViewHodler();
            view = LayoutInflater.from(context).inflate(R.layout.search_listitem, null);
            // 初始化三个textview显示蓝牙信息
            mHodler.deviceAddress = (TextView) view
                    .findViewById(R.id.tv_deviceAddr);
            mHodler.deviceName = (TextView) view
                    .findViewById(R.id.tv_deviceName);
            mHodler.rssi = (TextView) view.findViewById(R.id.tv_rssi);
            view.setTag(mHodler);
        } else {
            mHodler = (ViewHodler) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        mHodler.deviceAddress.setText(device.getAddress());
        mHodler.deviceName.setText(device.getName());
        mHodler.rssi.setText("" + rssis.get(i));

        return view;
    }

    private class ViewHodler {
        TextView deviceAddress;
        TextView deviceName;
        TextView rssi;

    }

    public void setHeaderViewData(){

    }

}